﻿using System;
using System.Collections.Generic;
using System.Text;

namespace System
{
    public static class StringEx
    {
        /// <summary>
        /// 十六进制转换为字节数组
        /// </summary>
        /// <param name="hexString"></param>
        /// <returns></returns>
        public static byte[] ToBytes(this string hexString)
        {
            hexString = hexString.Replace(" ", "");
            if ((hexString.Length % 2) != 0)
                hexString += " ";
            var returnBytes = new byte[hexString.Length / 2];
            for (int i = 0; i < returnBytes.Length; i++)
                returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16);
            return returnBytes;
        }
        /// <summary>
        /// 十六进制转换为字节数组
        /// </summary>
        /// <param name="hexString"></param>
        /// <returns></returns>
        public static byte[] ToByte(this string hexString)
        {
            hexString = hexString.Replace(" ", "");
            if ((hexString.Length % 2) != 0)
                hexString += " ";
            var returnBytes = new byte[hexString.Length / 2];
            for (int i = 0; i < returnBytes.Length; i++)
                returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16);
            return returnBytes;
        }



        /// <summary>
        /// 转换成ASCII码值
        /// </summary>
        /// <param name="Object"></param>
        /// <returns></returns>
        public static byte[] ToAscii(this string Object)
        {
            return Encoding.ASCII.GetBytes(Object);
        }


        /// <summary>
        /// 转换成ToUTF8码值
        /// </summary>
        /// <param name="Object"></param>
        /// <returns></returns>
        public static byte[] ToUtf8(this string Object)
        {
            return Encoding.UTF8.GetBytes(Object);
        }

        /// <summary>
        /// 转换成ToUTF8码值
        /// </summary>
        /// <param name="Object"></param>
        /// <returns></returns>
        public static byte[] ToGBK(this string Object)
        {
            return Encoding.GetEncoding("GBK").GetBytes(Object);
        }


        /// <summary>
        /// 转换成ToUTF8码值
        /// </summary>
        /// <param name="Object"></param>
        /// <returns></returns>
        public static byte[] ToGb2312(this string Object)
        {
            return Encoding.GetEncoding("gb2312").GetBytes(Object);
        }

        /// <summary>
        /// 转换成ASCII码值
        /// </summary>
        /// <param name="Object"></param>
        /// <param name="Length"> </param>
        /// <returns></returns>
        public static byte[] ToAscii(this string Object, int Length)
        {
            var s = new List<byte>();
            s.AddRange(Encoding.ASCII.GetBytes(Object));
            int re = Length - s.Count;
            if (re > 0)
            {
                for (int i = 0; i < re; i++)
                    s.Add(0);
            }
            return s.ToArray();
        }


    }

}
